package com.huaxinkaiye.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.huaxinkaiye.R;
import com.huaxinkaiye.application.HuaXinApplication;
import com.huaxinkaiye.bean.KongPositionBean;
import com.huaxinkaiye.bean.RowsBean;
import com.huaxinkaiye.lookbigphoto.PhotosDialog;
import com.huaxinkaiye.utils.ProgressUtil;
import com.huaxinkaiye.utils.SimpleClient;
import com.huaxinkaiye.utils.ToastUtil;
import com.huaxinkaiye.utils.UrlUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CarRuKuDengJiActivity extends Activity implements OnClickListener {
	private ImageView im_back, im_cancle, im_save;
	private Spinner daqu_spinner0,daqu_spinner, paishu_spinner, geshu_spinner,
			cengshu_spinner;
	private String picUrl="";
	public  NetworkImageView im_che;
	private TextView te_chepaihao, te_chuangjiantime, te_danganbianhao,te_daqu,te_paishu,te_geshu,te_chengshu;
	private RowsBean rowsBean;
	public ArrayList<String> arr1;
	public ArrayList<String> arr2;
	public ArrayList<String> arr3;
	public ArrayList<String> arr4;
	public List<NameValuePair> params;
	public ProgressUtil progress;
	public String s_1,s_2,s_3,s_4;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				jiexiListData(msg.obj.toString());
				break;
			case 2:
				Jixikongweizhi(msg.obj.toString());
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		progress = new ProgressUtil(this, "正在获取数据");
		setContentView(R.layout.activity_rukudengji);
		initView();
	}
	protected void Jixikongweizhi(String s) {     
		arr1 = new ArrayList<String>();
		try {
			JSONArray json=new JSONArray(s);
			Gson gson=new Gson();
			for(int i=0;i<json.length();i++){
				KongPositionBean bean=gson.fromJson(json.get(i).toString(),KongPositionBean.class);
				arr1.add(bean.getRegion()+bean.getRow()+bean.getNumber()+bean.getLayer());		
			}
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < arr1.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("text", arr1.get(i));
				data.add(map);
			}
			SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
					R.layout.spinner, new String[] { "text" },
					new int[] { R.id.textView1 });
			daqu_spinner0.setAdapter(simpleAdapter);
			progress.dismiss();
			  //注册事件  
			daqu_spinner0.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  

	            @Override  
	            public void onNothingSelected(AdapterView<?> parent) {  
	                Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();  
	            }

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					 Spinner spinner=(Spinner) arg0; 
				   CharSequence String;
				   String postionid=spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1);
					te_daqu.setText(postionid.substring(0, 1));
					te_paishu.setText(postionid.substring(1, 2));
					te_geshu.setText(postionid.substring(2, 3));
					te_chengshu.setText(postionid.substring(3, 4));
				 //Toast.makeText(getApplicationContext(), ""+spinner.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
				}  
	        }); 
		}catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	protected void Jiexikongweizhi1() {
		// TODO Auto-generated method stub
//		Log.e("kongweizhi--",s);
//		arr1 = new ArrayList<String>();
//		arr2 = new ArrayList<String>();;
//		arr3 = new ArrayList<String>();;
//		arr4 = new ArrayList<String>();;
//		try {
//			JSONArray json=new JSONArray(s);
//			Gson gson=new Gson();
//			for(int i=0;i<json.length();i++){
//				KongPositionBean bean=gson.fromJson(json.get(i).toString(),KongPositionBean.class);
//				arr1.add(bean.getRegion());
//				arr2.add(bean.getRow());
//				arr3.add(bean.getNumber());
//				arr4.add(bean.getLayer());
//			}
		    int i1 = 0,i2 = 0,i3 = 0,i4=0;
			final String arr1[] = new String[] { "A", "B", "C", "D", "E", "F", "G",
					"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
					"T", "U", "V", "W", "X", "Y", "Z" };
			final String arr2[] = new String[] { "1", "2", "3", "4", "5", "6", "7",
					"8", "9" };
			final String arr3[] = new String[] { "1", "2", "3", "4", "5", "6", "7",
					"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
					"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
					"W", "X", "Y", "Z" };
			final String arr4[] = new String[] { "A", "B", "C" };
			if(rowsBean.getStoreCode()!=null){
			for(int i=0;i<arr1.length;i++){
				if(rowsBean.getStoreCode().substring(0, 1).equals(arr1[i])){
					i1=i;
				}
			}	
			for(int i=0;i<arr2.length;i++){
				if(rowsBean.getStoreCode().substring(1, 2).equals(arr2[i])){
					i2=i;
				}
			}
			for(int i=0;i<arr3.length;i++){
				if(rowsBean.getStoreCode().substring(2, 3).equals(arr3[i])){
					i3=i;
				}
			}
			for(int i=0;i<arr4.length;i++){
				if(rowsBean.getStoreCode().substring(3, 4).equals(arr4[i])){
					i4=i;
				}
			}
			}
			// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
			// android.R.layout.simple_spinner_item, arr1);
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < arr1.length; i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("text", arr1[i]);
				data.add(map);
			}
			SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
					R.layout.spinner, new String[] { "text" },
					new int[] { R.id.textView1 });
			daqu_spinner.setAdapter(simpleAdapter);

			List<Map<String, Object>> data2 = new ArrayList<Map<String, Object>>();
			
			for (int i = 0; i < arr2.length; i++) {
				Map<String, Object> map2 = new HashMap<String, Object>();
				map2.put("text", arr2[i]);
				data2.add(map2);
			}
			SimpleAdapter simpleAdapter2 = new SimpleAdapter(this, data2,
					R.layout.spinner, new String[] { "text" },
					new int[] { R.id.textView1 });
			paishu_spinner.setAdapter(simpleAdapter2);

			List<Map<String, Object>> data3 = new ArrayList<Map<String, Object>>();
			
			for (int i = 0; i < arr3.length; i++) {
				Map<String, Object> map3 = new HashMap<String, Object>();
				map3.put("text", arr3[i]);
				data3.add(map3);
			}
			SimpleAdapter simpleAdapter3 = new SimpleAdapter(this, data3,
					R.layout.spinner, new String[] { "text" },
					new int[] { R.id.textView1 });
			geshu_spinner.setAdapter(simpleAdapter3);

			List<Map<String, Object>> data4 = new ArrayList<Map<String, Object>>();
			
			for (int i = 0; i < arr4.length; i++) {
				Map<String, Object> map4 = new HashMap<String, Object>();
				map4.put("text", arr4[i]);
				data4.add(map4);
			}
			SimpleAdapter simpleAdapter4 = new SimpleAdapter(this, data4,
					R.layout.spinner, new String[] { "text" },
					new int[] { R.id.textView1 });
			cengshu_spinner.setAdapter(simpleAdapter4);
			
			  //注册事件  
			daqu_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  

	            @Override  
	            public void onNothingSelected(AdapterView<?> parent) {  
	                Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();  
	            }

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					 Spinner spinner=(Spinner) arg0; 
					 s_1=spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1);
		             //Toast.makeText(getApplicationContext(), ""+spinner.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
				}  
	        }); 
			
			paishu_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  

	            @Override  
	            public void onNothingSelected(AdapterView<?> parent) {  
	                Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();  
	            }

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					 Spinner spinner=(Spinner) arg0; 
					// te_paishu.setText(spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1));
		           s_2=spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1);
					 // Toast.makeText(getApplicationContext(), ""+spinner.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
				}  
	        });
			
			geshu_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  

	            @Override  
	            public void onNothingSelected(AdapterView<?> parent) {  
	                Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();  
	            }

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					 Spinner spinner=(Spinner) arg0; 
					 //te_geshu.setText(spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1));
		            s_3=spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1);
					 // Toast.makeText(getApplicationContext(), ""+spinner.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
				}  
	        });
			
			cengshu_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  

	            @Override  
	            public void onNothingSelected(AdapterView<?> parent) {  
	                Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();  
	            }

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					 Spinner spinner=(Spinner) arg0; 
					 //te_chengshu.setText(spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1));
		            s_4=spinner.getItemAtPosition(arg2).toString().substring(6,spinner.getItemAtPosition(arg2).toString().length()-1);
					 // Toast.makeText(getApplicationContext(), ""+spinner.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
				}  
	        });		
			daqu_spinner.setSelection(i1); 
			paishu_spinner.setSelection(i2); 
			geshu_spinner.setSelection(i3); 
			cengshu_spinner.setSelection(i4);	
	}

	public  void jiexiListData(String s) {
		// TODO Auto-generated method stub
		progress.dismiss();
		Log.e("ruku_save_data", s);
		JSONObject json;
		try {
			json = new JSONObject(s);
			if(json.getBoolean("isSuccess")){
				ToastUtil.show(this, json.getString("message"));
				Intent intent=new Intent(this,CarRuKuListActivity.class);
				setResult(RESULT_OK,intent);
				finish();
			}else{
				ToastUtil.show(this, json.getString("message"));
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

	private void initView() {
		// TODO Auto-generated method stub
		im_back = (ImageView) findViewById(R.id.im_back);
		im_back.setOnClickListener(this);
		im_cancle = (ImageView) findViewById(R.id.im_cancle);
		im_cancle.setOnClickListener(this);
		im_save = (ImageView) findViewById(R.id.im_save);
		im_save.setOnClickListener(this);
		im_che=(NetworkImageView)findViewById(R.id.im_che);
		im_che.setOnClickListener(this);
		te_chepaihao = (TextView) findViewById(R.id.te_chepaihao);
		te_chuangjiantime = (TextView) findViewById(R.id.te_chuangjiantime);
		te_danganbianhao = (TextView) findViewById(R.id.te_danganbianhao);
		
//		te_daqu= (TextView) findViewById(R.id.te_daqu);
//		te_paishu= (TextView) findViewById(R.id.te_paishu);
//		te_geshu= (TextView) findViewById(R.id.te_geshu);
//		te_chengshu= (TextView) findViewById(R.id.te_chengshu);
		daqu_spinner0=(Spinner)findViewById(R.id.daqu_spinner0);
		daqu_spinner = (Spinner) findViewById(R.id.daqu_spinner);
		paishu_spinner = (Spinner) findViewById(R.id.paishu_spinner);
		geshu_spinner = (Spinner) findViewById(R.id.geshu_spinner);
		cengshu_spinner = (Spinner) findViewById(R.id.cengshu_spinner);
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		rowsBean = (RowsBean) getIntent().getSerializableExtra("rowsBeanInfo");
		for(int i=0;i<rowsBean.getPictures().size();i++){
			if(rowsBean.getPictures().get(i).getFileType()==1){
				picUrl=UrlUtils.host+rowsBean.getPictures().get(i).getFilepath();
				im_che.setImageUrl(picUrl, 
						HuaXinApplication.mImageLoader);
			}
		}
		te_chepaihao.setText("车牌号： " + rowsBean.getPlateNumber());
		te_chuangjiantime.setTag("创建时间： " + rowsBean.getCreateTime());
		te_danganbianhao.setText("档案编号： " + rowsBean.getBarCode());
		
		Jiexikongweizhi1();	
		//initspinner();
//		if(rowsBean.getStoreCode()!=null||(!rowsBean.getStoreCode().endsWith(""))){
//			te_daqu.setText(rowsBean.getStoreCode().substring(0, 1));
//			te_paishu.setText(rowsBean.getStoreCode().substring(1, 2));
//			te_geshu.setText(rowsBean.getStoreCode().substring(2, 3));
//			te_chengshu.setText(rowsBean.getStoreCode().substring(3, 4));
//		}
	}

	private void initspinner() {
		// TODO Auto-generated method stub
		if (HuaXinApplication.isNetWorkConnected()) {
		String url=UrlUtils.kongweizhi_url;
		progress.show();
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("", ""));
		new Thread(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				String data;
				try {
					data = SimpleClient.doPost(
								UrlUtils.kongweizhi_url, params);
					Log.e("kongweizhi_url_data", data);
					Message mes = new Message();
					mes.what = 2;
					mes.obj = data;
					handler.sendMessage(mes);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			}
			
		}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT)
					.show();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_back:
			Intent intent=new Intent(this,CarRuKuListActivity.class);
			setResult(RESULT_OK,intent);
			finish();
			break;
		case R.id.im_cancle:
			Intent intent2=new Intent(this,CarRuKuListActivity.class);
			setResult(RESULT_OK,intent2);
			finish();
			break;
		case R.id.im_save:
			Ruku();
			break;
		case R.id.im_che:
			if(picUrl.equals("")){
				ToastUtil.show(this, "没有图片");
				return;	}
			lookBigImage();	
			break;
		}
	}

	private void Ruku() {
		if (HuaXinApplication.isNetWorkConnected()) {
		progress.show();
		String url = UrlUtils.ruku_url ;
		Log.e("ruku_url", url);
		// TODO Auto-generated method stub
	//	if (HuaXinApplication.isNetWorkConnected()) {		
//			if(te_daqu.getText().toString().equals("")){
//				ToastUtil.show(this, "大区不为空");
//				return;
//			}
//			if(te_paishu.getText().toString().equals("")){
//				ToastUtil.show(this, "排数不为空");
//				return;
//			}
//			if(te_geshu.getText().toString().equals("")){
//				ToastUtil.show(this, "个数不为空");
//				return;
//			}
//			if(te_chengshu.getText().toString().equals("")){
//				ToastUtil.show(this, "层数不为空");
//				return;
//			}
	
			params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", rowsBean.getId() + ""));
			params.add(new BasicNameValuePair("status", rowsBean.getStatus() + ""));
			params.add(new BasicNameValuePair("bigarea", s_1));
			params.add(new BasicNameValuePair("rowno", s_2));
			params.add(new BasicNameValuePair("number", s_3));
			params.add(new BasicNameValuePair("plies", s_4));
			new Thread(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					try {
						//String data=SimpleClient.doPost(UrlUtils.ruku_url, params);
						String data=SimpleClient.doPost(UrlUtils.ruku_url, params);
						Log.e("ruku_save_data00", data);
						Message mes = new Message();
						mes.what = 1;
						mes.obj = data;
						handler.sendMessage(mes);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}.start();
	} else {
		Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT)
				.show();
	}
//			new Thread(){
//
//				@Override
//				public void run() {
//					// TODO Auto-generated method stub
//					super.run();
//				String data;
//				try {
//					data = SimpleClient.doPost(
//								UrlUtils.ruku_url, params);
//					Log.e("ruku_url_data", data);
//					Message mes = new Message();
//					mes.what = 1;
//					mes.obj = data;
//					handler.sendMessage(mes);
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}	
//				}
//				
//			}.start();
//			
		//} else {
      //    ToastUtil.show(this, "网络异常");
		//}
	}
	protected void lookBigImage() {
		// TODO Auto-generated method stub
		    final ArrayList<String> urllist = new ArrayList<String>();
			urllist.add(picUrl);
		    new PhotosDialog(CarRuKuDengJiActivity.this, R.style.Dialog).show(0,
				urllist);
	}
}
